<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" >
<head th:include="/include/commonHeader::commonHeader('不在位信息编辑')"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="col-sm-12">
        <form class="form-horizontal m-t" id="exampleForm">
            <input id="id" name="id"  type="hidden" th:value="${crewAbsent.id}">
            <div class="form-group">
                <label class="col-sm-3 control-label"><font style="color:red;">*</font>人员：</label>
                <div class="col-sm-8">
                    <select id="pCode" name="pCode" class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"><font style="color:red;">*</font>开始时间：</label>
                <div class="col-sm-8">
                    <input id="fromDate" name="fromDate" class="form-control" type="text" th:value="${crewAbsent.fromDate}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"><font style="color:red;">*</font>结束时间：</label>
                <div class="col-sm-8">
                    <input id="toDate" name="toDate" class="form-control" type="text" th:value="${crewAbsent.toDate}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">不在位类型：</label>
                <div class="col-sm-8">
                    <select id="absentType" name="absentType" th:value="${crewAbsent.absentType}" ></select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">不在位原因：</label>
                <div class="col-sm-8">
                    <input id="absentReason" name="absentReason" class="form-control" type="text" th:value="${crewAbsent.absentReason}">
                </div>
            </div>


            <div class="form-group">
                <div class="text-center">
                    <button id="btn_save" onclick="save()" type="button" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 自定义js -->
<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>
	<script th:inline="javascript">
        var prefix = "/crewAbsent";
        var isReadonly = "[[${isReadonly}]]";//页面是否只能查看
        $(function(){

            //初始化下列框
            var paramArr = [{'moduleFlag':[[${crewAbsent.moduleFlag}]]}];
            select2CommonAsync("#pCode","/select2/getStaffPCode.do","人员姓名/首字母",[[${crewAbsent.pCode}]],paramArr);
            select2InitForData("#absentType","/select2/getCodeType.do?moduleFlag=" + [[${crewAbsent.moduleFlag}]]  +"&parentCode=D1", [[${crewAbsent.absentType}]],null,false,null);
            //开始时间
            $("#fromDate").datetimepicker({
                language: 'zh-CN',
                format: 'yyyy-mm-dd hh:ii:00',
                autoclose: true,
                todayBtn: true,
                pickerPosition: "bottom-left"
            }).on('changeDate', function (ev) {
                var fromDate = $("#fromDate").val();
                $("#toDate").datetimepicker('setStartDate', fromDate);
                $("#fromDate").datetimepicker('hide');
                $("#editForm").bootstrapValidator('revalidateField', 'fromDate');
                $("#exampleForm").data('bootstrapValidator')
                    .updateStatus('fromDate', 'NOT_VALIDATED')
                    .validateField('fromDate')
                    .updateStatus('pCode', 'NOT_VALIDATED')
                    .validateField('pCode')
                    .updateStatus('toDate', 'NOT_VALIDATED')
                    .validateField('toDate');
            });

            $("#pCode").change(function () {
                $("#exampleForm").data('bootstrapValidator')
                    .updateStatus('pCode', 'NOT_VALIDATED')
                    .validateField('pCode')
                    .updateStatus('fromDate', 'NOT_VALIDATED')
                    .validateField('fromDate')
                    .updateStatus('toDate', 'NOT_VALIDATED')
                    .validateField('toDate');
            });

            $("#toDate").datetimepicker({
                language: 'zh-CN',
                format: 'yyyy-mm-dd hh:ii:00',
                autoclose: true,
                todayBtn: true,
                pickerPosition: "bottom-left"
            }).on('changeDate', function (ev) {
                var fromDate = $("#fromDate").val();
                var toDate = $("#toDate").val();
                $("#fromDate").datetimepicker('setEndDate', toDate);
                $("#toDate").datetimepicker('hide');
                $("#editForm").bootstrapValidator('revalidateField', 'toDate');
                $("#exampleForm").data('bootstrapValidator')
                    .updateStatus('fromDate', 'NOT_VALIDATED')
                    .validateField('fromDate')
                    .updateStatus('pCode', 'NOT_VALIDATED')
                    .validateField('pCode')
                    .updateStatus('toDate', 'NOT_VALIDATED')
                    .validateField('toDate');
            });

            if(isReadonly === "1"){
                readonlyInput();
            }else{
                initValidator();
            }
        });

        //禁用所有输入框
        function readonlyInput(){
            $("#exampleForm").find("input,textarea,checkbox,radio").attr("readonly",true);
            $("#exampleForm").find("select").prop("disabled",true);
        $("#btn_save").hide();//隐藏操作按钮
        }

        //保存
        function save(){
            $("#exampleForm").data('bootstrapValidator').validate();//触发一次所有的验证
            if(!$("#exampleForm").data('bootstrapValidator').isValid()){//判断验证是否已经通过
                return;
            }

            $.ajax({
                cache : true,
                type : "POST",
                url : prefix + "/update.do",
                data : $('#exampleForm').serialize(),
                async : false,
                error : function(request) {
                    parent.layer.alert("Connection error");
                },
                success : function(data) {
                    if (data.code == 0) {
                        parent.layer.msg("操作成功");
                        parent.reload();
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);
                    } else {
                        parent.layer.alert(data.msg)
                    }
                }
            });
        }

        //初始化验证规则
        function initValidator(){
           $('#exampleForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
               fields: {
               }
        });
        }
	</script>
</body>
</html>
